<?php

require_once 'authpage.php';

/**
 * Description of NewsfeedController
 *
 * @author user
 */
class NewsfeedController extends AuthpageController {

    public function index($id = null, $offset = 2) {
        // SET OFFSET
        if ($offset == null) {
            $offset = 10;
        }

        // GET NOTIF
        $notifs = new ORM('notif');
        $where = "receiver_id = " . $this->auth_user->user->id;
        if ($id != null) {
            $where .= " AND id < $id";
        }
        $notifs = $notifs->
                        order_by(array('id' => 'DESC'))->
                        where($where)->
                        limit(0, $offset)->find_all();

        // MAKE READ
        $this->make_read($notifs);

        // SET IME
        $time_strings = array();
        $i = 0;
        foreach ($notifs as $notif) {
            $notif->time = $this->calculate_time($notif->time_stamp);
            $time_strings[$i++] = $this->time_to_string($notif->time);
        }

        // SET VIEW
        $notifs_view = View::factory("newsfeed/notifs", array("notifs" => $notifs, "time_strings" => $time_strings));
        $this->view->content = View::factory("newsfeed/index", array("notifs_view" => $notifs_view));

        // AJAX REQUEST
        if (isset($_POST["ajax"]) && isset($_POST["partial"])) {
            $this->view = View::factory("ajax", array("content" => $notifs_view));
            return;
        } else if (isset($_POST["ajax"])) {
            $this->view = View::factory("ajax", array("content" => $this->view->content));
            return;
        }
    }

    public function newestfeed($last_id = null) {
        // GET NOTIFS
        $notifs = new ORM("notif");
        $where = "receiver_id = " . $this->auth_user->user->id;

        if ($last_id != null) {
            $where .= " AND id > $last_id";
        }

        $notifs = $notifs->where($where)->find_all();

        // MAKE READ
        $this->make_read($notifs);

        // SET IME
        $time_strings = array();
        $i = 0;
        foreach ($notifs as $notif) {
            $notif->time = $this->calculate_time($notif->time_stamp);
            $time_strings[$i++] = $this->time_to_string($notif->time);
        }

        // SET VIEW
        $notifs_view = View::factory("newsfeed/notifs", array("notifs" => $notifs, "time_strings" => $time_strings));

        // AJAX REQUEST
        if (isset($_POST["ajax"])) {
            $this->view = View::factory("ajax", array("content" => $notifs_view));
            return;
        }
    }

    public function unread() {
        $notifs = new ORM('notif');
        $notifs = $notifs->where("receiver_id = " . $this->auth_user->user->id . " AND unread = 1")->find_all();
        
        // MAKE READ
        //$this->make_read($notifs);
        
        $this->view = View::factory("ajax", array("content" => count($notifs)));
    }

    private function calculate_time($time_stamp) {
        return $seconds = (time() - $time_stamp);
    }

    private function time_to_string($seconds) {
        if ($seconds < 60) {
            if ($seconds < 2) {
                return floor($seconds) . " second ago";
            } else {
                return floor($seconds) . " seconds ago";
            }
        } else if ($seconds < 60 * 60) {
            $minutes = $seconds / 60;
            if ($minutes < 2) {
                return floor($minutes) . " minute ago";
            } else {
                return floor($minutes) . " minutes ago";
            }
        } else if ($seconds < 60 * 60 * 24) {
            $hours = $seconds / 60 / 60;
            if ($hours < 2) {
                return floor($hours) . " hour ago";
            } else {
                return floor($hours) . " hours ago";
            }
        } else {
            $days = $seconds / 60 / 60 / 24;
            if ($days < 2) {
                return floor($days) . " day ago";
            } else {
                return floor($days) . " days ago";
            }
        }
    }

    private function make_read($notifs) {
        $ids = array();

        foreach ($notifs as $n) {
            $ids[] = $n->id;
        }

        $where = "";
        $bound = count($ids) - 1;
        for ($i = 0; $i < $bound; ++$i) {
            $where .= "`id`=" . $ids[$i] . " OR ";
        }
        if ($bound >= 0) {
            $where .= "`id`=" . $ids[$i];
        }

        $query = "UPDATE `notif` SET `unread` = '0' WHERE $where";
        DBQuery::instance()->query($query);
    }

}

?>
